package visual;
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


@SuppressWarnings("serial")
public class ChoicePanel extends JPanel{
	private final ButtonGroup group;
	public static final String aleatoire = "Random number";
	public static final String proba = "In proportion to the surface area of the environment";
	public static final String userChoice = "Choose the number";
	public static final String desc = "Choose the creature generation strategy : ";
	
	private final JLabel valueSliderChoiceNb;
	private final JLabel valueSliderPercentage;
	private String title;
	
	public ChoicePanel(String title) {
		super(new BorderLayout());
		int nbChoice = 3;
		this.title = title;
		JRadioButton[] choices = new JRadioButton[nbChoice];
		group = new ButtonGroup();
			
		choices[0] = new JRadioButton(aleatoire);
		choices[0].setActionCommand(aleatoire);
		
		choices[1] = new JRadioButton(proba);
		choices[1].setActionCommand(proba);
		
		choices[2] = new JRadioButton(userChoice);
		choices[2].setActionCommand(userChoice);
		
		for (int i = 0; i < nbChoice; i++)
            group.add(choices[i]);

		choices[0].setSelected(true);
		
		JSlider sliderNbPlant = new JSlider(JSlider.HORIZONTAL,
                0, 100, 50);
		valueSliderChoiceNb = new JLabel(Integer.toString(50));
		Dimension labelUserChoise = new Dimension(35, 50);
		valueSliderChoiceNb.setPreferredSize(labelUserChoise);
		valueSliderChoiceNb.setMinimumSize(labelUserChoise);
		valueSliderChoiceNb.setMaximumSize(labelUserChoise);
        
		sliderNbPlant.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				int value = ((JSlider) e.getSource()).getValue();
				valueSliderChoiceNb.setText(Integer.toString(value));
			}
		});
		
		
		JSlider sliderPercentage = new JSlider(JSlider.HORIZONTAL,
                0, 100, 50);
		valueSliderPercentage = new JLabel("50%");
		Dimension labelPercentageDimension = new Dimension(40, 50);
		valueSliderPercentage.setPreferredSize(labelPercentageDimension);
		valueSliderPercentage.setMinimumSize(labelPercentageDimension);
		valueSliderPercentage.setMaximumSize(labelPercentageDimension);
        
		sliderPercentage.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				int value = ((JSlider) e.getSource()).getValue();
				valueSliderPercentage.setText(Integer.toString(value) + "%");
			}
		});
				
		createPanel(this.title, choices, sliderNbPlant, valueSliderChoiceNb,
				sliderPercentage, valueSliderPercentage);
	}

	/**
	 * @param title
	 * @param choices
	 * @param sliderNbPlant
	 * @param valueSliderNbPlant
	 * @param sliderPercentage
	 * @param valueSliderPercentage
	 */
	private void createPanel(String title, JRadioButton[] choices,
			JSlider sliderNbPlant, final JLabel valueSliderNbPlant,
			JSlider sliderPercentage, final JLabel valueSliderPercentage) {
		
		JPanel box = new JPanel();
		JLabel labelDesc= new JLabel(desc);
		
		box.setLayout(new BoxLayout(box, BoxLayout.PAGE_AXIS));
		box.add(labelDesc);
		
		//Choice : percentage
		JPanel panPercentage = new JPanel();
		panPercentage.setLayout(new BoxLayout(panPercentage, BoxLayout.LINE_AXIS));
		panPercentage.add(valueSliderPercentage);
		panPercentage.add(sliderPercentage);
		panPercentage.setBorder(BorderFactory.createEmptyBorder(0,50,0,10));
		
		//Choice : number define by user
		JPanel panUserChoice = new JPanel();
		panUserChoice.setLayout(new BoxLayout(panUserChoice, BoxLayout.LINE_AXIS));
		panUserChoice.add(valueSliderNbPlant);
		panUserChoice.add(sliderNbPlant);
		panUserChoice.setBorder(BorderFactory.createEmptyBorder(0,60,0,10));
		box.add(panUserChoice);
				
		box.add(choices[0]);
		box.add(choices[1]);
		box.add(panPercentage);
		box.add(choices[2]);
		box.add(panUserChoice);
					
		box.setBorder(BorderFactory.createTitledBorder(title));
		
		add(box,BorderLayout.PAGE_START);
	}

	public ButtonGroup getGroup() { return group;}
	
	public int getValueSliderChoiceNb() { return Integer.valueOf(valueSliderChoiceNb.getText());}
	public int getValueSliderPercentage() { return Integer.valueOf(valueSliderPercentage.getText().substring(0, valueSliderPercentage.getText().length()-1));};
	
	public String getTitle(){return title;}
}
